#include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 301;
int a[N][N];
int c[N][N][101];
inline void add(int x, int y, int k, int color) {
  for (int i = x; i <= n; i += i & -i)
    for (int j = y; j <= m; j += j & -j) c[i][j][color] += k;
}
inline int query(int x, int y, int color) {
  int ret = 0;
  for (int i = x; i; i -= i & -i)
    for (int j = y; j; j -= j & -j) ret += c[i][j][color];
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  int q, x1, x2, y1, y2, color;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      scanf("%d", &color);
      a[i][j] = color;
      add(i, j, 1, color);
    }
  for (scanf("%d", &q); q--;) {
    int op;
    scanf("%d", &op);
    if (op == 1) {
      scanf("%d%d%d", &x1, &y1, &color);
      add(x1, y1, -1, a[x1][y1]);
      a[x1][y1] = color;
      add(x1, y1, 1, color);
    } else {
      scanf("%d%d%d%d%d", &x1, &x2, &y1, &y2, &color);
      printf("%d\n", query(x2, y2, color) - query(x1 - 1, y2, color) -
                         query(x2, y1 - 1, color) +
                         query(x1 - 1, y1 - 1, color));
    }
  }
}
